package com.coderbean.demo.leaveapply.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.coderbean.demo.leaveapply.service.ITLeaveApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coderbean.demo.Result;
import com.coderbean.demo.leaveapply.entity.TLeaveApplyModel;

@RestController
@RequestMapping("t_leave_apply")
public class TLeaveApplyAction {
	
	@Autowired
	private ITLeaveApplyService service;

	@GetMapping("/list")
	public Result<?> list (TLeaveApplyModel model, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			   HttpServletRequest req) {
		LambdaQueryWrapper<TLeaveApplyModel> queryWrapper = new LambdaQueryWrapper<TLeaveApplyModel>(model);
		Page<TLeaveApplyModel> page = new Page<TLeaveApplyModel>(pageNo, pageSize);
		IPage<TLeaveApplyModel> pageList = service.page(page, queryWrapper);
	    return new Result<>(pageList);
	}
	
	@PostMapping("/add")
	public Result<?> add (@RequestBody TLeaveApplyModel model) {
		model.setCreateTime(new Date());
		service.save(model);
		return new Result<>("添加成功", model);
	}
	
	
	@GetMapping("/get/{id}")
	public Result<TLeaveApplyModel> get(@PathVariable("id") String id) {
		TLeaveApplyModel model = service.getById(id);
		return new Result<>(model);
	}
	
	
	@PutMapping("/edit")
	public Result<?> edit (@RequestBody TLeaveApplyModel model) {
		service.updateById(model);
		return new Result<>("编辑成功", model);
	}
	
	
	@DeleteMapping("/delete")
	public Result<String> delete (@RequestParam("id") String id) {
		service.removeById(id);
		return new Result<>("删除成功");
	}
	
	
}
